﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using MongoDB.Driver;
using Sustainalytics.ClientDW.Entities;
using Sustainalytics.ESG.Entities;
using Sustainalytics.MongoDB.Extensions;
using Sustainalytics.Synchronization.Interface;
using Xunit;
using Xunit.Abstractions;

namespace Sustainalytics.CRT.Synchronization.Tests.Integration
{
    public class RPIndicatorsGroupingTest
    {
        private readonly ITestOutputHelper output;
        public readonly SyncContext syncContext = TestSyncContextFactoy.Create();

        public RPIndicatorsGroupingTest(ITestOutputHelper output)
        {
            this.output = output;
        }

        [Fact]

        public async Task GroupingTest()
        {
            var companyIds = new[] { Guid.Parse("f68f4165-c58a-4d03-aa80-496291bcb635") };
            var controversyIndicators =
              await
                  MongoFactory.GetCollectionFromDatabase<RPIndicatorExtended>(
                          syncContext.GAConnectionStrings["ClientDW"])
                      .Aggregate()
                      .Match(ci => companyIds.Contains(ci.CompanyId) && ci.Template.IsControversyIndicator)
                      .Group(ci => ci.CompanyId,
                          g => new { _id = g.Key, Indicators = g.Select(x => x) }).ToListAsync();

            foreach (var indicator in controversyIndicators)
            {
                output.WriteLine(indicator.ToString());
            }
        }
    }
}
